using System;
using System.Web.Routing;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using SoftwareMonkeys.Scriptech.Diagnostics;

namespace SoftwareMonkeys.Scriptech.Web.Projections
{
	public class ProjectionRouteHandler : IRouteHandler
	{
		public string ScriptName = String.Empty;

		public ProjectionRouteHandler ()
		{
			Initialize();
		}
		
		public void Initialize()
		{
			using (LogGroup logGroup = LogGroup.StartDebug("Initializing script route handler."))
			{
				VirtualPath = WebUtilities.ConvertAbsoluteUrlToApplicationRelativeUrl(HttpContext.Current.Request.Url.ToString());
				
				LogWriter.Debug("Virtual path: " + VirtualPath);
			}
		}

		/*public string GetScriptName ()
		{
			string url = HttpContext.Current.Request.Url.ToString ();
			
			string baseUrl = WebUtilities.ConvertApplicationRelativeUrlToAbsoluteUrl ("/Scripts") + "/";
			
			string script = url.Replace (baseUrl, "");
			
			
			// If prepared properly then return the script
			if (script != String.Empty && script.IndexOf (":") == -1) {
				return script;
			} else
				// Otherwise no script was detected
				return String.Empty;
		}*/

		public string VirtualPath { get; private set; }

		public IHttpHandler GetHttpHandler (RequestContext requestContext)
		{
			Initialize();
			
			return new ProjectionHttpHandler (this.VirtualPath);
			//IHttpHandler page = BuildManager.CreateInstanceFromVirtualPath (VirtualPath, typeof(Page)) as IHttpHandler;
			//page.
			//return page;
			
		}
	}
}

